var loginTips = $('#loginTips');
$('#blogSubmit').click(function () {

    var oldpassword = $("#oldpassword").val();
    var newpassword = $("#newpassword").val();
    var repassword = $("#repassword").val();

    if(oldpassword == '' || oldpassword == null || newpassword == '' || newpassword == null || repassword == '' || repassword == null) {
        showTips("数据不能为空")
        return;
    }
    if(newpassword.length < 6) {
        showTips("标题长度不能小于6")
        return;
    }
    if(newpassword != repassword) {
        showTips("两次密码输入不一致")
        return;
    }
    hideTips();

    var param = {oldpassword:oldpassword,newpassword:newpassword,repassword:repassword}
    $.ajax({
        url:"/user/resetPw",
        type:"post",
        data:param,
        success:function(data){
                showTips(data.msg);
        },
        error:function () {
            showTips('发送错误！');
        }
    });
})

function showTips(msg) {
    loginTips.show();
    loginTips.text(msg);
}
function hideTips() {
    loginTips.text('');
    loginTips.hide();
}


var IMAGE_BASE_URL = $('#IMAGE_BASE_URL').val();

/**
 * 上传头像
 */
$('#btnUp').click(function () {
    $('#btn_file').click();
});

$("#btn_file").on("change",upfile);

function upfile(){
    var form = new FormData(document.getElementById("form1"));
    $.ajax({
        url:"/user/upImg",
        type:"post",
        data:form,
        processData:false,
        contentType:false,
        success:function(data){
            //上传成功后改变<img>标签的src地址，id参数其实不需要，主要是为了刷新img的图片资源。
            $("#avatar").attr("src",IMAGE_BASE_URL+data);
            //重新绑定事件，防止二次上传时失效
            $("#avatar").on("change",upfile);
        }
    });
}